package com.hjb.toolbardemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.hjb.toolbardemo.R;
import com.hjb.toolbardemo.base.BaseActivity;
import com.hjb.toolbardemo.fragment.ForumTabFragment;
import com.hjb.toolbardemo.fragment.HomeTabFragment;
import com.hjb.toolbardemo.fragment.MineTabFragment;
import com.hjb.toolbardemo.fragment.StoresTabFragment;
import com.hjb.toolbardemo.utils.UtilResources;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 作者:  hjb
 * 时间: 2016/2/17.
 */
public class MainActivity_V4 extends BaseActivity {

    @InjectView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @InjectView(R.id.main_tab_host)
    FragmentTabHost mainTabHost;
    @InjectView(R.id.main_tab_wrapper)
    public LinearLayout mMainTabWrapper;
    private Class tabHostFragment[] = {HomeTabFragment.class, StoresTabFragment.class, ForumTabFragment.class, MineTabFragment.class};
    private int tabHostImg[] = {R.drawable.tab_1_btn, R.drawable.tab_2_btn, R.drawable.tab_3_btn, R.drawable.tab_4_btn};
    private String[] tabHostNames = UtilResources.getStringArray(R.array.main_tab_host);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_v4);
        ButterKnife.inject(this);
        setBackGone();
        setTitleTv("首页");
        init();
    }

    public void init() {
        mainTabHost.setup(this, getSupportFragmentManager(), R.id.fragment_container);
        for (int i = 0; i < tabHostFragment.length; i++) {
            initTitle(i);
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mainTabHost.newTabSpec(tabHostNames[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mainTabHost.addTab(tabSpec, tabHostFragment[i], null);
            TabWidget tabWidget = mainTabHost.getTabWidget();
            tabWidget.setGravity(Gravity.CENTER_VERTICAL);
            tabWidget.setDividerDrawable(null);
            //可设置Tab区域的背景
            //mainTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.选择器);
        }
        mainTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTitleTv(tabId);
            }
        });
    }

    private void initTitle(int index) {
        setBackGone();
        switch (index) {
            case 0:
                setTitleTv("首页");
                break;
            case 1:
                setTitleTv("商家");
                break;
            case 2:
                setTitleTv("论坛");
                break;
            case 3:
                setTitleTv("我的");
                break;

        }
    }

    private long clickTime = 0; //记录第一次点击的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - clickTime) > 2000) {
                Toast.makeText(this, "再按一次后退出程序",
                        Toast.LENGTH_SHORT).show();
                clickTime = System.currentTimeMillis();
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private View getTabItemView(int index) {
        View view = View.inflate(this, R.layout.main_tab_item, null);
        ImageView tabimg = (ImageView) view.findViewById(R.id.tab_img);
        TextView tabtv = (TextView) view.findViewById(R.id.tab_tv);
        tabimg.setImageResource(tabHostImg[index]);
        tabtv.setText(tabHostNames[index]);
        return view;
    }

    /**
     * 提供给首页,商家,论坛,我的之间相互切换方法
     *
     * @param index
     */
    public void switchFragment(int index) {
        mainTabHost.setCurrentTab(index);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 12) {
                (new MineTabFragment()).onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}